<template>
    <div class="home" style=" padding: 10px">
        <!--  功能区域  -->
        <div class="flex-bar">
            <!--  搜索区域  -->
            <div style="margin: 10px 0">
                <el-button type="primary" @click="add">今日打卡</el-button>
            </div>
        </div>
        <el-table
                :data="tableData"
                border
                stripe
                style="width: 100%">
            <el-table-column
                    prop="id"
                    label="打卡记录ID"
                    sortable/>
            <el-table-column
                    prop="healthStatus"
                    label="健康状况"/>
            <el-table-column
                    prop="isGoOut"
                    label="是否外出"/>
            <el-table-column
                    prop="temperature"
                    label="体温"/>
            <el-table-column
                    prop="time"
                    label="打卡时间"/>
            <el-table-column
                    prop="workingStatus"
                    label="工作状态"/>
            <el-table-column
                    prop="remark"
                    label="备注"/>
        </el-table>

        <!--  分页模块  -->
        <div style="margin: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!--  一层弹窗  -->
        <el-dialog
                :title="title"
                v-model="dialogVisible"
                width="600px">
            <el-form :model="form" label-width="120px" :rules="rules">
                <el-form-item label="健康状况" prop="healthStatus">
                    <el-input v-model="form.healthStatus" style="width: 80%;"></el-input>
                </el-form-item>
                <el-form-item label="体温" prop="temperature">
                    <el-input v-model="form.temperature" style="width: 80%;"></el-input>
                </el-form-item>
                <el-form-item label="是否外出" prop="temperature">
                    <el-input v-model="form.isGoOut" style="width: 80%;"></el-input>
                </el-form-item>
                <el-form-item label="工作状态" prop="temperature">
                    <el-input v-model="form.workingStatus" style="width: 80%;"></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="temperature">
                    <el-input v-model="form.remark" style="width: 80%;"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
          </span>
            </template>
        </el-dialog>
    </div>

</template>

<style>
    .flex-bar {
        display: flex;
        flex-direction: row;
        align-items: center;
    }
</style>

<script>

    import request from "../utils/request";

    export default {
        name: 'Register',
        components: {},
        data() {
            return {
                rules: {
                    healthStatus: [
                        {required: true, message: '请输入健康状况', trigger: 'blur'},
                    ],
                    temperature: [
                        {required: true, message: '请输入体温', trigger: 'blur'},
                    ],
                    isGoOut: [
                        {required: true, message: '请输入是否外出', trigger: 'blur'},
                    ],
                    workingStatus: [
                        {required: true, message: '请输入工作状态', trigger: 'blur'},
                    ],
                },
                form: {
                    healthStatus: '健康',
                    temperature: 36.5,
                    isGoOut: '否',
                    workingStatus: '居家工作',
                },
                dialogVisible: false,
                currentPage: 1,
                pageSize: 10,
                tableData: [],
                user: {},
                title: '',
                money: 0,
                healthStatus: '',
                isGoOut: '',
                workingStatus: '',
                temperature: 36.5,
                handleSizeChange: 5,
                handleCurrentChange: 10,
                url: require('@/assets/img/zfb.jpg'),
            }
        },
        created() {
            let ownerStr = sessionStorage.getItem("user") || "{}";
            this.user = JSON.parse(ownerStr);

            //请求服务端，确认当前登录用户的合法信息
            request.get("/user/" + this.user.id).then(res => {
                if (res.code === '0') {
                    this.user = res.data;
                }
            });
            this.load();
        },
        methods: {
            load() {      //登陆或进行任何数据操作后，都应该调用load方法刷新网页
                if (this.user.role === 1) {    //登陆的是房主，则看到自己的房屋的信息
                    request.get("/register/ownerHouse", {
                        params: {
                            pageNum: this.currentPage,
                            pageSize: this.pageSize,
                            userId: this.user.id,
                        }
                    }).then(res => {
                        console.log(res);
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    })
                }
            },
            add() {    //点击“新增”按钮
                this.dialogVisible = true;
                this.title = '填写健康信息';
            },
            save() {    //点击一层弹窗的确认按钮，则进入二层弹窗
                this.form.userId = this.user.id;
                this.form.time = new Date();
                if (this.form.healthStatus != null && this.form.temperature != null) {
                    request.post("/register", this.form).then(res => {
                        console.log(res);
                        if (res.code == '0') {
                            this.$message({
                                type: "success",
                                message: "新增成功"
                            });
                            this.load();   //刷新表格的数据
                            this.dialogVisible = false;  //关闭弹窗
                        } else {
                            this.$message({
                                type: "error",
                                message: res.msg
                            })
                        }
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "请填写打卡所需的所有信息！"
                    });
                    this.dialogVisible = true;
                }
            }
        },
        handleEdit(row) {
            this.form = JSON.parse(JSON.stringify(row));
            this.title = '编辑房屋信息';
            this.dialogVisible = true;
            this.$nextTick(() => {
                if (this.$refs['upload']) {
                    this.$refs['upload'].clearFiles();  //清除历史文件列表
                }
            })
        },
        handleDelete(id) {
            console.log(id);
            request.delete("/register/" + id).then(res => {
                if (res.code == '0') {
                    this.$message({
                        type: "success",
                        message: "删除成功"
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: res.msg
                    })
                }
                this.load();
            })
        },
        handleSizeChange(pageSize) {    //改变当前每页个数触发
            this.pageSize = pageSize;
            this.load();
        },
        handleCurrentChange(pageNum) {  //改变当前页码触发
            this.currentPage = pageNum;
            this.load();
        }
    }
</script>